import request from '@/config/axios'

export enum SpuStatus {
  'ENABLE' = 1,
  'DISABLE' = 2
}

export interface SpuVO {
  id: string
  spuName: string
  spuDesc: string
  detail: string
  coverUrl: string
  picUrls: string
  originalPrice: number
  salePrice: number
  totalStock: number
  isShowStock: boolean
  salesCount: number
  virtualSalesCount: number
  status: SpuStatus // 1-上架 2-下架
  sortNo: number
  spuType: number // 1-普通商品 2-课程商品
  createTime: number
  updateTime: number
  creator: string
  updater: string
}

export type SpuDetailVO = SpuVO & {
  detail: string
  picUrls: string
  videoUrl: string
}

export type SpuDTO = Partial<SpuVO>

export type SpuCreateDTO = Partial<
  Omit<SpuDetailVO, 'id' | 'createTime' | 'updateTime' | 'creator' | 'updater'>
>

// id必传，除了其他参数选传，不传的参数不会更新
export type SpuUpdateDTO = Pick<SpuDetailVO, 'id'> &
  Partial<Omit<SpuDetailVO, 'createTime' | 'updateTime' | 'creator' | 'updater'>>

const baseURL = '/train/product-spu'

// 查询商品spu列表
export const getSpuPage = async (params: PageParam & SpuDTO) => {
  return await request.get({ url: `${baseURL}/page`, params })
}

// 查询商品spu详情
export const getSpu = async (id: string) => {
  return await request.get({ url: `${baseURL}/get-detail-by-id?spuId=` + id })
}

// 新增商品spu
export const createSpu = async (data: SpuVO) => {
  return await request.post({ url: `${baseURL}/create`, data })
}

// 修改商品spu
export const updateSpu = async (data: SpuUpdateDTO) => {
  return await request.post({ url: `${baseURL}/update`, data })
}

// 删除商品spu
export const deleteSpu = async (id: string) => {
  return await request.delete({ url: `${baseURL}/delete?id=` + id })
}

// 导出商品spu Excel
export const exportSpu = async (params) => {
  return await request.download({ url: `${baseURL}/export-excel`, params })
}
